home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / sunrpc / pmap_rmt.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  11KB  |  391 lines

  1. /* @(#)pmap_rmt.c    2.2 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * pmap_rmt.c
  36.  * Client interface to pmap rpc service.
  37.  * remote call and broadcast service
  38.  *
  39.  * Copyright (C) 1984, Sun Microsystems, Inc.
  40.  */
  41.  
  42. #include <rpc/rpc.h>
  43. #include <rpc/pmap_prot.h>
  44. #include <rpc/pmap_clnt.h>
  45. #include <rpc/pmap_rmt.h>
  46. #include <sys/socket.h>
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include <net/if.h>
  50. #include <sys/ioctl.h>
  51. #include <arpa/inet.h>
  52. #define MAX_BROADCAST_SIZE 1400
  53.  
  54. extern int errno;
  55. static struct timeval timeout = { 3, 0 };
  56.  
  57.  
  58. /*
  59.  * pmapper remote-call-service interface.
  60.  * This routine is used to call the pmapper remote call service
  61.  * which will look up a service program in the port maps, and then
  62.  * remotely call that routine with the given parameters.  This allows
  63.  * programs to do a lookup and call in one step.
  64. */
  65. enum clnt_stat
  66. pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
  67.     struct sockaddr_in *addr;
  68.     u_long prog, vers, proc;
  69.     xdrproc_t xdrargs, xdrres;
  70.     caddr_t argsp, resp;
  71.     struct timeval tout;
  72.     u_long *port_ptr;
  73. {
  74.     int socket = -1;
  75.     register CLIENT *client;
  76.     struct rmtcallargs a;
  77.     struct rmtcallres r;
  78.     enum clnt_stat stat;
  79.  
  80.     addr->sin_port = htons(PMAPPORT);
  81.     client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
  82.     if (client != (CLIENT *)NULL) {
  83.         a.prog = prog;
  84.         a.vers = vers;
  85.         a.proc = proc;
  86.         a.args_ptr = argsp;
  87.         a.xdr_args = xdrargs;
  88.         r.port_ptr = port_ptr;
  89.         r.results_ptr = resp;
  90.         r.xdr_results = xdrres;
  91.         stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
  92.             xdr_rmtcallres, &r, tout);
  93.         CLNT_DESTROY(client);
  94.     } else {
  95.         stat = RPC_FAILED;
  96.     }
  97.     (void)close(socket);
  98.     addr->sin_port = 0;
  99.     return (stat);
  100. }
  101.  
  102.  
  103. /*
  104.  * XDR remote call arguments
  105.  * written for XDR_ENCODE direction only
  106.  */
  107. bool_t
  108. xdr_rmtcall_args(xdrs, cap)
  109.     register XDR *xdrs;
  110.     register struct rmtcallargs *cap;
  111. {
  112.     u_int lenposition, argposition, position;
  113.  
  114.     if (xdr_u_long(xdrs, &(cap->prog)) &&
  115.         xdr_u_long(xdrs, &(cap->vers)) &&
  116.         xdr_u_long(xdrs, &(cap->proc))) {
  117.         lenposition = XDR_GETPOS(xdrs);
  118.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  119.             return (FALSE);
  120.         argposition = XDR_GETPOS(xdrs);
  121.         if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
  122.             return (FALSE);
  123.         position = XDR_GETPOS(xdrs);
  124.         cap->arglen = (u_long)position - (u_long)argposition;
  125.         XDR_SETPOS(xdrs, lenposition);
  126.         if (! xdr_u_long(xdrs, &(cap->arglen)))
  127.             return (FALSE);
  128.         XDR_SETPOS(xdrs, position);
  129.         return (TRUE);
  130.     }
  131.     return (FALSE);
  132. }
  133.  
  134. /*
  135.  * XDR remote call results
  136.  * written for XDR_DECODE direction only
  137.  */
  138. bool_t
  139. xdr_rmtcallres(xdrs, crp)
  140.     register XDR *xdrs;
  141.     register struct rmtcallres *crp;
  142. {
  143.     caddr_t port_ptr;
  144.  
  145.     port_ptr = (caddr_t)crp->port_ptr;
  146.     if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
  147.         xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
  148.         crp->port_ptr = (u_long *)port_ptr;
  149.         return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
  150.     }
  151.     return (FALSE);
  152. }
  153.  
  154.  
  155. /*
  156.  * The following is kludged-up support for simple rpc broadcasts.
  157.  * Someday a large, complicated system will replace these trivial 
  158.  * routines which only support udp/ip .
  159.  */
  160.  
  161. static int
  162. getbroadcastnets(addrs, sock, buf)
  163.     struct in_addr *addrs;
  164.     int sock;  /* any valid socket will do */
  165.     char *buf;  /* why allocxate more when we can use existing... */
  166. {
  167.     struct ifconf ifc;
  168.         struct ifreq ifreq, *ifr;
  169.     struct sockaddr_in *sin;
  170.         int n, i;
  171.  
  172.         ifc.ifc_len = UDPMSGSIZE;
  173.         ifc.ifc_buf = buf;
  174.         if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
  175.                 perror("broadcast: ioctl (get interface configuration)");
  176.                 return (0);
  177.         }
  178.         ifr = ifc.ifc_req;
  179.         for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
  180.                 ifreq = *ifr;
  181.                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  182.                         perror("broadcast: ioctl (get interface flags)");
  183.                         continue;
  184.                 }
  185.                 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
  186.             (ifreq.ifr_flags & IFF_UP) &&
  187.             ifr->ifr_addr.sa_family == AF_INET) {
  188.             sin = (struct sockaddr_in *)&ifr->ifr_addr;
  189. #ifdef SIOCGIFBRDADDR   /* 4.3BSD */
  190.             if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
  191.                 addrs[i++] = inet_makeaddr(inet_netof
  192.                  /* Changed to pass struct instead of s_addr member
  193.                    by roland@gnu.  */
  194.                 (sin->sin_addr), INADDR_ANY);
  195.             } else {
  196.                 addrs[i++] = ((struct sockaddr_in*)
  197.                   &ifreq.ifr_addr)->sin_addr;
  198.             }
  199. #else /* 4.2 BSD */
  200.             addrs[i++] = inet_makeaddr(inet_netof
  201.               (sin->sin_addr.s_addr), INADDR_ANY);
  202. #endif
  203.         }
  204.     }
  205.     return (i);
  206. }
  207.  
  208. typedef bool_t (*resultproc_t)();
  209.  
  210. enum clnt_stat 
  211. clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
  212.     u_long        prog;        /* program number */
  213.     u_long        vers;        /* version number */
  214.     u_long        proc;        /* procedure number */
  215.     xdrproc_t    xargs;        /* xdr routine for args */
  216.     caddr_t        argsp;        /* pointer to args */
  217.     xdrproc_t    xresults;    /* xdr routine for results */
  218.     caddr_t        resultsp;    /* pointer to results */
  219.     resultproc_t    eachresult;    /* call with each result obtained */
  220. {
  221.     enum clnt_stat stat;
  222.     AUTH *unix_auth = authunix_create_default();
  223.     XDR xdr_stream;
  224.     register XDR *xdrs = &xdr_stream;
  225.     int outlen, inlen, fromlen, nets;
  226.     register int sock;
  227.     int on = 1;
  228. #ifdef FD_SETSIZE
  229.     fd_set mask;
  230.     fd_set readfds;
  231. #else
  232.     int readfds;
  233.     register int mask;
  234. #endif /* def FD_SETSIZE */
  235.     register int i;
  236.     bool_t done = FALSE;
  237.     register u_long xid;
  238.     u_long port;
  239.     struct in_addr addrs[20];
  240.     struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
  241.     struct rmtcallargs a;
  242.     struct rmtcallres r;
  243.     struct rpc_msg msg;
  244.     struct timeval t; 
  245.     char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
  246.  
  247.     /*
  248.      * initialization: create a socket, a broadcast address, and
  249.      * preserialize the arguments into a send buffer.
  250.      */
  251.     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  252.         perror("Cannot create socket for broadcast rpc");
  253.         stat = RPC_CANTSEND;
  254.         goto done_broad;
  255.     }
  256. #ifdef SO_BROADCAST
  257.     if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
  258.         perror("Cannot set socket option SO_BROADCAST");
  259.         stat = RPC_CANTSEND;
  260.         goto done_broad;
  261.     }
  262. #endif /* def SO_BROADCAST */
  263. #ifdef FD_SETSIZE
  264.     FD_ZERO(&mask);
  265.     FD_SET(sock, &mask);
  266. #else
  267.     mask = (1 << sock);
  268. #endif /* def FD_SETSIZE */
  269.     nets = getbroadcastnets(addrs, sock, inbuf);
  270.     bzero((char *)&baddr, sizeof (baddr));
  271.     baddr.sin_family = AF_INET;
  272.     baddr.sin_port = htons(PMAPPORT);
  273.     baddr.sin_addr.s_addr = htonl(INADDR_ANY);
  274. /*    baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
  275.     (void)gettimeofday(&t, (struct timezone *)0);
  276.     msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
  277.     t.tv_usec = 0;
  278.     msg.rm_direction = CALL;
  279.     msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  280.     msg.rm_call.cb_prog = PMAPPROG;
  281.     msg.rm_call.cb_vers = PMAPVERS;
  282.     msg.rm_call.cb_proc = PMAPPROC_CALLIT;
  283.     msg.rm_call.cb_cred = unix_auth->ah_cred;
  284.     msg.rm_call.cb_verf = unix_auth->ah_verf;
  285.     a.prog = prog;
  286.     a.vers = vers;
  287.     a.proc = proc;
  288.     a.xdr_args = xargs;
  289.     a.args_ptr = argsp;
  290.     r.port_ptr = &port;
  291.     r.xdr_results = xresults;
  292.     r.results_ptr = resultsp;
  293.     xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
  294.     if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
  295.         stat = RPC_CANTENCODEARGS;
  296.         goto done_broad;
  297.     }
  298.     outlen = (int)xdr_getpos(xdrs);
  299.     xdr_destroy(xdrs);
  300.     /*
  301.      * Basic loop: broadcast a packet and wait a while for response(s).
  302.      * The response timeout grows larger per iteration.
  303.      */
  304.     for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
  305.         for (i = 0; i < nets; i++) {
  306.             baddr.sin_addr = addrs[i];
  307.             if (sendto(sock, outbuf, outlen, 0,
  308.                 (struct sockaddr *)&baddr,
  309.                 sizeof (struct sockaddr)) != outlen) {
  310.                 perror("Cannot send broadcast packet");
  311.                 stat = RPC_CANTSEND;
  312.                 goto done_broad;
  313.             }
  314.         }
  315.         if (eachresult == NULL) {
  316.             stat = RPC_SUCCESS;
  317.             goto done_broad;
  318.         }
  319.     recv_again:
  320.         msg.acpted_rply.ar_verf = _null_auth;
  321.         msg.acpted_rply.ar_results.where = (caddr_t)&r;
  322.                 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
  323.         readfds = mask;
  324.         switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, 
  325.                    (int *)NULL, &t)) {
  326.  
  327.         case 0:  /* timed out */
  328.             stat = RPC_TIMEDOUT;
  329.             continue;
  330.  
  331.         case -1:  /* some kind of error */
  332.             if (errno == EINTR)
  333.                 goto recv_again;
  334.             perror("Broadcast select problem");
  335.             stat = RPC_CANTRECV;
  336.             goto done_broad;
  337.  
  338.         }  /* end of select results switch */
  339.     try_again:
  340.         fromlen = sizeof(struct sockaddr);
  341.         inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
  342.             (struct sockaddr *)&raddr, &fromlen);
  343.         if (inlen < 0) {
  344.             if (errno == EINTR)
  345.                 goto try_again;
  346.             perror("Cannot receive reply to broadcast");
  347.             stat = RPC_CANTRECV;
  348.             goto done_broad;
  349.         }
  350.         if (inlen < sizeof(u_long))
  351.             goto recv_again;
  352.         /*
  353.          * see if reply transaction id matches sent id.
  354.          * If so, decode the results.
  355.          */
  356.         xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
  357.         if (xdr_replymsg(xdrs, &msg)) {
  358.             if ((msg.rm_xid == xid) &&
  359.                 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
  360.                 (msg.acpted_rply.ar_stat == SUCCESS)) {
  361.                 raddr.sin_port = htons((u_short)port);
  362.                 done = (*eachresult)(resultsp, &raddr);
  363.             }
  364.             /* otherwise, we just ignore the errors ... */
  365.         } else {
  366. #ifdef notdef
  367.             /* some kind of deserialization problem ... */
  368.             if (msg.rm_xid == xid)
  369.                 fprintf(stderr, "Broadcast deserialization problem");
  370.             /* otherwise, just random garbage */
  371. #endif
  372.         }
  373.         xdrs->x_op = XDR_FREE;
  374.         msg.acpted_rply.ar_results.proc = xdr_void;
  375.         (void)xdr_replymsg(xdrs, &msg);
  376.         (void)(*xresults)(xdrs, resultsp);
  377.         xdr_destroy(xdrs);
  378.         if (done) {
  379.             stat = RPC_SUCCESS;
  380.             goto done_broad;
  381.         } else {
  382.             goto recv_again;
  383.         }
  384.     }
  385. done_broad:
  386.     (void)close(sock);
  387.     AUTH_DESTROY(unix_auth);
  388.     return (stat);
  389. }
  390.  
  391.